import resCompont from "./Res";
cc.Class({
    extends: cc.Component,

    properties: {

        preHeight:0,
        subPaddingLeft:0,
        pre:cc.SpriteFrame, //数字前面的图片
        subFrame:cc.SpriteFrame, //后面的的图片
        images:[cc.SpriteFrame],
        num:0,
        res:resCompont,
        originX:Number,
        originY:Number
        

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

        this.images = this.res.nums;
        this.node.width = 0;
        this.originX = this.node.x;
        this.originY = this.node.y; 

    },


    setText:function(text){

        this.images = this.res.nums;
        this.num = text;
        this.node.removeAllChildren();
        this.node.width = 0;
        this.node.x = this.originX;
        this.node.y = this.originY;
        
        let preImg
        
        if (this.pre) {
            preImg = new cc.Node();//创建数字前面的节点
            let sprite = preImg.addComponent(cc.Sprite);//为该节点前加上脚本
            let rect = this.pre.getRect();//获取节点的轮扩
            sprite.spriteFrame = this.pre;//为该节点提供展示图

            preImg.y = 0;//该节点在y初始化0
            preImg.width = rect.width;
            preImg.height = rect.height;//节点的宽高为图片的宽高

            this.node.addChild(preImg);//加入该子节点
            sprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
            sprite.type = cc.Sprite.Type.SIMPLE;

            preImg.width = preImg.width / preImg.height * this.preHeight; //相对父节点的百分比
            preImg.x = preImg.width / 2;//节点的横坐标为图片中心
            preImg.height = this.preHeight;//纵坐标属性编译器已经固定
            this.node.width = preImg.width;//节点的宽度等于图片的宽度

        }

        for( let j=(text + '').length-1; j >= 0; j--){
            //text+""相当于计算数字字符串的长度
            let digit = parseInt(text / (Math.pow(10, j)) % 10 + '');//去每个位数上的数字
            let digitImg = new cc.Node();
            let sp = digitImg.addComponent(cc.Sprite);
          
            let rect = this.images[digit].getRect();
            sp.spriteFrame = this.images[digit];
       
            sp.sizeMode = cc.Sprite.SizeMode.CUSTOM;
            sp.type = cc.Sprite.Type.SIMPLE;
         
            digitImg.width = rect.width / rect.height * this.node.height;
            digitImg.height = this.node.height;
            digitImg.x = this.node.width + digitImg.width / 2; 
            digitImg.y = 0;
            this.node.width += digitImg.width;
            this.node.addChild(digitImg);
        }

        let offset = this.node.width / 2;//所以数字横坐标偏移长度
     
        for (let child of this.node.children) {
            child.x -= offset;//每个数字横坐标偏移一定的距离
        }
       
    },

    setLeftText:function(text,minus){

        this.images = this.res.nums;
        this.num = text;
        this.node.removeAllChildren();
        this.node.width = 0;
        this.node.x = this.originX;
        this.node.y = this.originY;
        
        let preImg
        
        if (minus) {
            
            preImg = new cc.Node();//创建数字前面的节点
            let sprite = preImg.addComponent(cc.Sprite);//为该节点前加上脚本
            let rect = this.pre.getRect();//获取节点的轮扩
            sprite.spriteFrame = this.pre;//为该节点提供展示图

            preImg.y = 0;//该节点在y初始化0
            preImg.width = rect.width;
            preImg.height = rect.height;//节点的宽高为图片的宽高

            this.node.addChild(preImg);//加入该子节点
            sprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
            sprite.type = cc.Sprite.Type.SIMPLE;

            preImg.width = 21; //相对父节点的百分比
            preImg.x = preImg.width / 2;//节点的横坐标为图片中心
            preImg.height =8;//纵坐标属性编译器已经固定
            this.node.width = preImg.width;//节点的宽度等于图片的宽度

        }

        for( let j=(text + '').length-1; j >= 0; j--){
            //text+""相当于计算数字字符串的长度
            let digit = parseInt(text / (Math.pow(10, j)) % 10 + '');//去每个位数上的数字
            let digitImg = new cc.Node();
            let sp = digitImg.addComponent(cc.Sprite);
          
            let rect = this.images[digit].getRect();
            sp.spriteFrame = this.images[digit];
       
            sp.sizeMode = cc.Sprite.SizeMode.CUSTOM;
            sp.type = cc.Sprite.Type.SIMPLE;
         
            digitImg.width = rect.width / rect.height * this.node.height;
            digitImg.height = this.node.height;
            digitImg.x = this.node.width + digitImg.width / 2; 
            digitImg.y = 0;
            this.node.width += digitImg.width;
            this.node.addChild(digitImg);
           

        }

        // let offset = -this.node.width/2;//所以数字横坐标偏移长度
     
        // for (let child of this.node.children) {
        //     child.x -= offset;//每个数字横坐标偏移一定的距离
        // }
       
    },


    start () {

    },

});
